﻿using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace STEditor.Classes.Tests
{
    [TestClass]
    public class STGameXamlWriterTests
    {

        private DirectoryInfo _folder = null;

        private const string TilesFolder = @"C:\Users\Rune\Documents\Visual Studio 2010\Projects\SilverTileApp\Assets";

        [TestInitialize]
        public void Initialize()
        {
            _folder = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
        }

        [TestCleanup]
        public void CleanUp()
        {
            if (_folder.Exists)
            {
                _folder.Delete(true);
            }
        }

        [TestMethod]
        public void Writer_Save_Should_Take_STGame_And_Folder_As_Arguments()
        {

            var saveGame = new STGame();
            new STGameXamlWriter().Save(saveGame, _folder.FullName);
            
        }


        [TestMethod]
        public void Writer_Save_Should_Create_Structure_On_Disk()
        {

            var saveGame = new STGame();
            new STGameXamlWriter().Save(saveGame, _folder.FullName);

            var tilesFolder = new DirectoryInfo(Path.Combine(_folder.FullName, STGameXamlWriter.TilesFolderName));
            Assert.IsTrue(tilesFolder.Exists, "Tiles folder was not created");

            var gameDataFile = new FileInfo(Path.Combine(_folder.FullName, STGameXamlWriter.GameDataFileName));
            Assert.IsTrue(gameDataFile.Exists, "GameData file was not created");

        }

        [TestMethod]
        public void Writer_Save_Should_Create_Tiles_For_All_Used_TileImages()
        {

            string tileName1 = "ZeldaTiles-0001-(0-0).png";
            string tileName2 = "ZeldaTiles-0002-(1-0).png";

            var saveGame = new STGame();
            var tile1 = saveGame.TileTypes.Add(Path.Combine(TilesFolder, tileName1));
            var tile2 = saveGame.TileTypes.Add(Path.Combine(TilesFolder, tileName2));

            var gameArea = new STGameArea { TileWidth = 32, TileHeight = 32, Width = 2, Height = 1, AreaName = "Area1" };
            gameArea.SetTile(0, 0, tile1);
            gameArea.SetTile(1, 0, tile2);
            saveGame.GameAreas.Add(gameArea);

            new STGameXamlWriter().Save(saveGame, _folder.FullName);

            var expectedTileFile1 = new FileInfo(Path.Combine(_folder.FullName, STGameXamlWriter.TilesFolderName, tileName1));
            var expectedTileFile2 = new FileInfo(Path.Combine(_folder.FullName, STGameXamlWriter.TilesFolderName, tileName2));

            Assert.IsTrue(expectedTileFile1.Exists, "Tile1 was not written to Tiles folder");
            Assert.IsTrue(expectedTileFile2.Exists, "Tile2 was not written to Tiles folder");

            var numExportedTiles = Directory.GetFiles(Path.Combine(_folder.FullName, STGameXamlWriter.TilesFolderName)).Count();

            Assert.AreEqual(2, numExportedTiles, "Expected 2 tiles to be exported. Actual: " + numExportedTiles);

        }
       
    }
}
